#
# plotBootstrapConleyTaber.R
#
# Plot results of Conley Taber bootstrap
# permutation test.
#

require(data.table)

# Variables in order of 
# Table 4: Difference-in-differences effects of primary reform on individual contributions. 
vars <- c("sumcontribs","log_sumcontribs","ncontribs","log_ncontribs","ndonors","log_ndonors","pct_primary")

var.labs = c("Sum of Contributions","Log Sum Contributions","Count of Contributions","Log Count Contributions","Count of Contributors","Log Count Contributors","Percent In Primary")

#
# Loop over separate bootstrap data files.
# Data created in BootstrapConleyTaber.do.
#

pdf("FigureA01AndA02.pdf",width=7,height=5)
par.old = par(mar=c(3.1,3.1,2.1,1.1))
for (i in 1:length(vars)) {
  # Call data.
  DT = fread(sprintf("ConleyTaber-%s.csv",vars[i]))
  
  # Null distribution of less-costly primary effect.
  DT[,plot(ecdf(x=boot_less_costly_primary),main="Less-costly primary",xlab="",ylab="ECDF",xlim=range(c(boot_less_costly_primary,actual_less_costly_primary),na.rm=T))]
  title(xlab=var.labs[i],ylab="Cumulative distribution",line=2)
  abline(h=c(0.05,0.95),lty=2,col='gray')
  # Vertical line at estimated effect.
  DT[1,abline(v=actual_less_costly_primary,lwd=3,col=1)]
  
  # Null distribution of nonpartisan primary effect.
  DT[,plot(ecdf(x=boot_nonpartisan_primary),main="Nonpartisan primary",xlab="",ylab="ECDF",xlim=range(c(boot_nonpartisan_primary,actual_nonpartisan_primary),na.rm=T))]
  title(xlab=var.labs[i],ylab="Cumulative distribution",line=2)
  abline(h=c(0.05,0.95),lty=2,col='gray')
  # Vertical line at estimated effect.
  DT[1,abline(v=actual_nonpartisan_primary,lwd=3,col=1)]
}
par(par.old)
dev.off()
